package com.common.base.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.common.base.R


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：4/5/2023
 *
 * 描述：数字显示器
 *
 * 修订历史：
 *
 */
class CusCountTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    View(context, attrs, defStyleAttr) {
    private var mCtvFontSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 100f, resources.displayMetrics).toInt()
    private var mCtvNum = 5
    private var mCtvBgColor = -0x400c09
    private var mCtvTxtColor = Color.WHITE
    private var mPaint: Paint? = null//主画笔
    private var mTxtPaint: Paint? = null
    private var mRect: Rect? = null
    private var mStr: String? = null
    private var mOffSet = 0

    init {
        val a = context.obtainStyledAttributes(attrs, R.styleable.CusCountTextView)
        mCtvFontSize =
            a.getDimension(R.styleable.CusCountTextView_z_ctv_font_size, mCtvFontSize.toFloat())
                .toInt()
        mCtvNum = a.getInteger(R.styleable.CusCountTextView_z_ctv_num, mCtvNum)
        mCtvTxtColor = a.getColor(R.styleable.CusCountTextView_z_txt_color, mCtvTxtColor)
        mCtvBgColor = a.getColor(R.styleable.CusCountTextView_z_bg_color, mCtvBgColor)
        a.recycle()
        init()
    }

    private fun init() {
        //初始化主画笔
        mTxtPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mTxtPaint?.color = mCtvTxtColor
        mTxtPaint?.textSize = mCtvFontSize.toFloat()
        mRect = Rect()
        mStr = mCtvNum.toString()
        if (mCtvNum >= 1000) {
            mStr = "999+"
        }
        mTxtPaint?.getTextBounds(mStr, 0, mStr?.length ?: 1, mRect)
        val aChartLen = (mRect?.width() ?: 1) / (mStr?.length ?: 1)
        mOffSet = (((mStr?.length ?: 1) - 1) * aChartLen * 0.7f).toInt()
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint?.color = mCtvBgColor
        mPaint?.strokeWidth = (mRect?.height() ?: 1).toFloat()
        mPaint?.strokeCap = Paint.Cap.ROUND
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mRect?.let {
            //文字左侧距圆心的偏移
            val offsetX = it.height() - it.width() / 2 + mOffSet
            setMeasuredDimension(2 * offsetX + it.width(), 2 * it.height())
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.save()
        mTxtPaint?.let { it1 ->
            mRect?.let {
                val offsetX = it.height() - it.width() / 2 + mOffSet
                canvas.translate(offsetX.toFloat(), (it.height() / 2).toFloat())
                //圆角矩形左上点
                val topX = it.width() / 2 - it.height()
                val topY = -it.height() / 2
                //圆角矩形右下点
                val bottomX = it.height() + it.width() / 2
                val bottomY = it.height() / 2 + it.height()
                canvas.drawRoundRect(
                    (topX - mOffSet).toFloat(),
                    topY.toFloat(),
                    (bottomX + mOffSet).toFloat(),
                    bottomY.toFloat(),
                    it.height().toFloat(),
                    it.height().toFloat(),
                    it1
                )
                canvas.drawText(mStr ?: "0", 0f, it.height().toFloat(), it1)
            }
        }
        canvas.restore()
    }

}